package com.cczhang.port;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Kill extends JFrame {

    private JLabel logLabel;
    private JLabel versionLabel;
    private JScrollPane logScrollPane;
    private static JTextArea logTextArea;
    private JTextField portInput;
    private JButton close;
    private JPanel rightPanel;
    private JLabel title = new JLabel("端口:");

    public Kill() {
        this.setSize(550, 360);
        this.setLocation(100, 100);
        logLabel = new JLabel("Test");
        versionLabel = new JLabel("Version 1.0.0  Gitee地址:https://gitee.com/Selected-Activities/gitee-7th-event-3 ");
        logScrollPane = new JScrollPane();
        logTextArea = new JTextArea();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        logLabel.setText("正在启动程序");
        logTextArea.setColumns(20);
        logTextArea.setRows(5);
        logScrollPane.setViewportView(logTextArea);
        portInput = new JTextField();
        close = new JButton("释放该端口");
        setLayout(null);
        versionLabel.setBounds(3, 300, 500, 20);
        versionLabel.setForeground(Color.RED);
        logLabel.setBounds(3, 2,380,20);
        logScrollPane.setBounds(4, 30,380,270);
        title.setBounds(405, 200, 35, 25);
        portInput.setBounds(445, 200, 80, 25);
        close.setBounds(405, 230, 120, 25);
        rightPanel = new JPanel();
        rightPanel.setBounds(405,30,120,140);

        int[] port = new int[]{3306,8080,6379,80,23,21,9090,3389,1521,25};
        for(int p : port){
            Button jButton = new Button(p+"");
            rightPanel.add(jButton);
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    execute(Integer.parseInt(e.getActionCommand()));
                }
            });
        }

        add(rightPanel);
        add(title);
        add(portInput);
        add(versionLabel);
        add(logLabel);
        add(logScrollPane);
        add(close);
        // 设置关闭时什么也不做
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        // 监听关闭按钮的点击操作
        this.addWindowListener(new WindowAdapter() {
            // new 一个WindowAdapter 类 重写windowClosing方法
            // WindowAdapter是个适配器类 具体看jdk的帮助文档
            public void windowClosing(WindowEvent e) {
                // 这里写对话框
                if (JOptionPane.showConfirmDialog(null, "退出", "提示", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }

        });
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    Integer.parseInt(portInput.getText());
                    execute(Integer.parseInt(portInput.getText()));
                }catch (Exception e1){
                    out("端口:"+portInput.getText()+"输入不正确!");
                }
            }
        });
        this.setVisible(true);
    }

    public void execute(int p){
        out("================================");
        out("请求关闭端口:"+p);
        out("正在查询:"+p);
        try{
            KillServer killServer = new KillServer();
            killServer.start(p);
        }catch (Exception e1){
            out("端口:"+p+"输入不正确!");
        }
    }
    public static void out(String str){
        logTextArea.append(str+"\r\n");
        logTextArea.setCaretPosition(logTextArea.getText().length());
    }


    public static void main(String[] args) {
        Kill cmd = new Kill();
        cmd.setTitle("端口释放");
        cmd.logLabel.setForeground(Color.red);
        cmd.logLabel.setText("正在启动. . .");
        try {
            cmd.logLabel.setForeground(Color.green);
            cmd.logLabel.setText("启动完成");
            Thread.sleep(900);
            cmd.logLabel.setForeground(Color.blue);
            cmd.logLabel.setText("正在输出运行日志");
            logTextArea.append("正在启动. . .");
        } catch (Exception e) {
            out(e.toString());
        }
    }
}
